<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="{{url_for('static',filename='js/jquery.min.js')}}"></script>
    <!--<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>-->
    <script src="{{url_for('static',filename='js/tradeSystemAccountInfo.js')}}"></script>
    <script src="{{url_for('static',filename='js/tradeSystemBuySellInfo.js')}}"></script>
    <script src="{{url_for('static',filename='js/tradeSystemEntrustInfo.js')}}"></script>
    <script src="{{url_for('static',filename='js/tradeSystemCapitalInfo.js')}}"></script>
    <script src="{{url_for('static',filename='js/bootstrap.min.js')}}"></script>
    <script src="{{url_for('static',filename='js/jquery.dataTables.min.js')}}"></script>
    <!--<script src="https://cdn.datatables.net/1.10.18/js/jquery.dataTables.min.js"></script>-->
    <!--<link rel="stylesheet" href="{{url_for('static',filename='css/bootstrap.min.css')}}">-->
    <link rel="stylesheet" href="{{url_for('static',filename='css/jquery.dataTables.min.css')}}">
    <!--<link rel="stylesheet" href="https://cdn.datatables.net/1.10.18/css/jquery.dataTables.min.css">-->
    <!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">-->
    <link rel="stylesheet" href="{{url_for('static',filename='css/bootstrap.min.css')}}">
    <title>实盘交易</title>
    <style>
    .button[disabled] {
        opacity: 0.6;
        cursor: not-allowed;
    }
    #block2{height:23px;width:500px;border:#ccc 1px solid;font-size:12px;text-align:center;overflow:hidden;}
    #block2 p{height:14px;}
    </style>

</head>
<body>
        <nav class="navbar navbar-default" role="navigation">
    <div class="container-fluid">
    <div class="navbar-header">
        <a class="navbar-brand" href="#">盯盘宝后台管理系统</a>
    </div>
    <div>
        <ul class="nav navbar-nav">
            <li ><a href="{{ url_for('start_web') }}">首页</a></li>
            <li ><a href="{{ url_for('permission_manage') }}">权限配置</a></li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                    管理员{{ session['admin'] if session['admin'] else "未登录" }}
                    <b class="caret"></b>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="{{ url_for("login_out") }}">退出</a></li>
                </ul>
            </li>
        </ul>
    </div>
    </div>
</nav>
<div id="all_content" style="margin:auto; width:80%">
    <button type="button" id ="refresh_adapter" class="btn btn-primary" onclick="refreshAdapter()">强制刷新账户</button>

<!--账户信息-->
<div id="capital_info">
    <h3>账户信息</h3>

    <div style="margin:auto">
    <div class="input-group col-xs-12">
    <select class="select_id_date form-control" style="margin:0px 20px 0px 0px;width:auto;">

    <!--<option value="严格策略">张小平实盘账户</option>-->

    </select>
    <select class="select_id_risk form-control" style="margin:0px 20px 0px 0px;width:auto;">

    <option value="低风险">低风险</option>
    <option value="高风险">高风险</option>
    </select>
    <select class="select_id_capital form-control" style="margin:0px 20px 0px 0px;width:auto;">

    <!--<option value="严格策略">张小平实盘账户</option>-->

    </select>


        </div>
            <br/>
            <br/>
    <div id="capitalDataTable" class="table table-striped table-bordered" cellspacing="0" width="80%">
    </div>
    </div>


</div>
<!--账户信息-->
<!--账户持仓   -->
<div id="account_info">

    <h3>账户持仓</h3>

    <br/>
    <br/>
    <div style="margin:auto">

    <div id="dataTable" class="table table-striped table-bordered" cellspacing="0" width="80%">
    </div>
    </div>
</div>

<!--今日买卖记录-->
<div id="buy_sell_info">
    <h3>今日成交</h3>

    <div style="margin:auto">


    <select id = "buy_or_sell" class="select_id_buy_sell form-control" style="margin:0px 20px 0px 0px;width:auto;">
    <option value="全部">全部</option>
    <option value="买入">买入</option>
    <option value="卖出">卖出</option>

    </select>


        <!--</div>-->
            <br/>
            <br/>
    <div id="buySellDataTable" class="table table-striped table-bordered" cellspacing="0" width="80%">
    </div>
    </div>


</div>
<!--今日买卖记录-->

<!--今日委托-->
<div id="entrust_info">
    <h3>今日委托</h3>

    <div style="margin:auto">

    <div id="entrustDataTable" class="table table-striped table-bordered" cellspacing="0" width="80%">
    </div>
    </div>


</div>
<!--今日委托-->

</div>

<script type="text/javascript">

    function simulateTradeRecords(){
        window.open("./simulateTrade");
    }

    function goToMonitor(){
        window.open("./monitor");
    }

    function manualOperation(){
        window.open("./manualOperation");
    }

    function historyProfit(){
        window.open("./historyProfit");
    }

    function threadMonitor(){
        window.open("./threadMonitor");
    }

    function scheduleTask(){
        window.open("./scheduleTask");
    }

    function clientManage(){
        window.open("./clientManage");
    }

    function hotMatch(){
        window.open("./hotMatch");
    }

    function stockPool(){
        window.open("./stockPool");
    }

    var dataTableAccount = null;
    var dataTableBuySell = null;
    var high_risk_account_list = null;
    var low_risk_account_list = null;
    var high_risk_user_name_list = null;
    var low_risk_user_name_list = null;


    // load some data
    function doLoad(portfoli_list,flag) {

        if (flag=='account_info'){
            dataTableAccount.data(portfoli_list,'stock_code');
            }
        if (flag=='buy_sell_info'){
            dataTableBuySell.data(portfoli_list,'stock_code');
            }
        if (flag=='entrust_info'){
            dataTableEntrust.data(portfoli_list,'stock_code');
            }
        if (flag == 'capital_info'){
            dataTableCapital.data(portfoli_list,'stock_code');
        }

    }

    function refreshAdapter(){
        $("#refresh_adapter").attr("disabled","true");
        setTimeout(function () {
           $("#refresh_adapter").removeAttr("disabled");
        },15000);
        $.ajax({
        url: '/refreshAdapter',
        async:true,
        success:function(result){
            setTimeout(function () {
               refreshAccountInfo();
            },3000);
            }
        });
    }


    function refreshAccountInfo(){

        var account=$(".select_id_capital").val();
        var date = $(".select_id_date").val();
        $.ajax({
                url: '/accountInfo',
                async:true,
                type:'POST',
                data:JSON.stringify({'account':account,'date':date}),
                dataType:'json',
                success:function(result){

                    <!--var obj=JSON.parse(result);-->
                    var obj = result;
                    var portfolio_list = obj.portfolio_list;

                    doLoad(portfolio_list,'account_info');

                }
            }
        );

}


    function refreshBuySellInfo(){
        var account=$(".select_id_capital").val();
        var date = $(".select_id_date").val();
        $.ajax({
                url: '/buySellRecords',
                async:true,
                type:'POST',
                data:JSON.stringify({'account':account,'date':date}),
                dataType:'json',
                success:function(result){

                    var buySellConditionValue = $("#buy_or_sell").val();

                    <!--var obj = JSON.parse(result);-->
                    var obj = result;
                    var buy_sell = obj.buy_sell;

                    var filtered_buy_sell = [];
                    if (buySellConditionValue == '全部'){
                        filtered_buy_sell = buy_sell;
                    }
                    if (buySellConditionValue == '买入'){
                        for (var i =0; i < buy_sell.length;i++){

                            if (buy_sell[i]['buySellMark'] == '买入' || buy_sell[i]['buySellMark'] == '证券买入'){
                                filtered_buy_sell.push(buy_sell[i]);
                            }
                        }
                    }
                    if (buySellConditionValue == '卖出'){
                        for (var i =0; i < buy_sell.length;i++){

                            if (buy_sell[i]['buySellMark'] == '卖出' || buy_sell[i]['buySellMark'] == '证券卖出'){
                                filtered_buy_sell.push(buy_sell[i]);
                            }
                        }

                    }
                    doLoad(filtered_buy_sell,'buy_sell_info');


                }
            }
        );


}

    function refreshEntrustInfo(){
        var account = $(".select_id_capital").val();
        var date = $(".select_id_date").val();
        $.ajax({
                url: '/entrustInfo',
                async:true,
                type:'POST',
                data:JSON.stringify({'account':account,'date': date}),
                dataType:'json',
                success:function(result){


                    <!--var obj = JSON.parse(result);-->
                    var obj = result;
                    var entrust = obj.entrust;


                    doLoad(entrust,'entrust_info');


                }
            }
        );
}


    function refreshCapitalInfo(){
        var account=$(".select_id_capital").val();
        var date = $(".select_id_date").val();
        $.ajax({
                url: '/capitalInfo',
                async:true,
                type:'POST',
                data:JSON.stringify({'account':account,'date':date}),
                dataType:'json',
                success:function(result){

                    <!--var obj=JSON.parse(result);-->
                    var obj = result;
                    var capital=obj.capital;

                    doLoad(capital,'capital_info');

                }
            }
        );

}


    function refreshAllInfo(){

        refreshAccountInfo();
        refreshBuySellInfo();
        refreshEntrustInfo();
        refreshCapitalInfo();

    }
    function changeAccountRisk() {
        if ($(".select_id_risk").val()=='低风险'){
            var accountList = low_risk_account_list;
            var userNameList = low_risk_user_name_list;
        }else{
            var accountList = high_risk_account_list;
            var userNameList = high_risk_user_name_list;
        };
        var htmlStr = "";
        for (var i=0;i<accountList.length;i++){
            var account = accountList[i];
            var accountName = userNameList[i];
            htmlStr += "<option value='" + account + "'>" + accountName + "</option>";
        }
        $('.select_id_capital').html(htmlStr);
    }
    function loadAccount(){

        $.getJSON("/accountJson",function (data) {
            high_risk_account_list=data.high_risk_account_list;
            low_risk_account_list= data.low_risk_account_list;
            high_risk_user_name_list=data.high_risk_user_name_list;
            low_risk_user_name_list= data.low_risk_user_name_list;
            changeAccountRisk();
        });
        $.getJSON("/json",function (data) {
            var tradeDayList=data.trade_day;
            for (var i=0;i<tradeDayList.length;i++){
                var date=tradeDayList[i];
                if (i==tradeDayList.length-1){
                    var html_str = "<option selected='selected' value='" + date + "'>" + date + "</option>";
                }else{
                    var html_str = "<option value='" + date + "'>" + date + "</option>";
                }
                $('.select_id_date').append(html_str);
            }
        });

    }

    jQuery(document).ready(function () {
          loadAccount();
          $(".select_id").change(function(){
            refreshAccountInfo();
          });

           $(".select_id_buy_sell").change(function(){
            refreshBuySellInfo();
          });

            $(".select_id_entrust").change(function(){
            refreshEntrustInfo();
          });

        $(".select_id_risk").change(function(){
                changeAccountRisk();
                refreshAccountInfo();
                refreshBuySellInfo();
                refreshEntrustInfo();
                refreshCapitalInfo();
          });
            $(".select_id_capital").change(function(){

                refreshAccountInfo();
                refreshBuySellInfo();
                refreshEntrustInfo();
                refreshCapitalInfo();
          });
        $(".select_id_date").change(function(){

                refreshAccountInfo();
                refreshBuySellInfo();
                refreshEntrustInfo();
                refreshCapitalInfo();
          });


         dataTableAccount = jQuery("#dataTable").rayTableOne({
            datasource: { data: [], keyfield: 'stock_code' },
            columns: [
            { field: "account_id", title: "账户ID"},
                { field: "stock_code", title: "证券代码"},
                { field: "name", title: "证券名称"},
                { field: "buyTime", title: "买入时间"},
                { field: "amount", title: "最新市值"},
                { field: "volume", title: "持仓量"},
                <!--{ field: "volBuy", title: "今买数量"},-->
                { field: "volSellable", title: "可卖数量"},
                { field: "profit", title: "浮动盈亏"},
                { field: "percent", title: "盈亏比例(%)"},
                { field: "buyPrice", title: "买入均价"},
                { field: "currentPrice", title: "当前价"}
                <!--{ title: "人工卖出", icons: [{ glyph: "glyphicon-trash", handler: iconAction, data: "stock_code" }-->
                <!--] }-->

            ],
            pagesize: 100,
            maxPageButtons: 5,
            rowNumbers: true

        });


    function doSell(s_id,code,amount,sellPrice) {

        $.ajax({
                url:'/sell',
                async:true,
                type:'POST',
                data:JSON.stringify({'s_id':s_id,'code':code,'amount':amount,'sell_price':sellPrice}),
                dataType:'json',
                success:function(result){
                    obj = result;
                    if (obj.message){
                        alert(obj.message);

                    }
            }

          });

    }




         dataTableBuySell = jQuery("#buySellDataTable").raytable_new({
            datasource: { data: [], keyfield: 'stock_code' },
            columns: [
            { field: "account_id", title: "账户ID"},
                { field: "code", title: "证券代码"},
                { field: "name", title: "证券名称"},
                { field: "orderid", title: "委托编号"},
                { field: "volume", title: "成交数量"},
                { field: "price", title: "成交价格"},
                { field: "time", title: "成交时间"},
                { field: "buySellMark", title: "买卖标志"},
                { field: "abstract", title: "摘要信息"}

            ],
            pagesize: 100,
            maxPageButtons: 5,
            rowNumbers: true

        });


         dataTableEntrust = jQuery("#entrustDataTable").raytable_new_new({
            datasource: { data: [], keyfield: 'stock_code' },
            columns: [
            { field: "account_id", title: "账户ID"},
                { field: "code", title: "证券代码"},
                { field: "name", title: "证券名称"},
                { field: "orderid", title: "委托编号"},
                { field: "time", title: "委托时间"},
                { field: "priceOrder", title: "委托价格"},
                { field: "volOrder", title: "委托数量"},
                { field: "volCancel", title: "撤单数量"},
                { field: "volTransaction", title: "成交数量"},
                { field: "buySellMark", title: "买卖标志"}
                <!--{ title: "人工撤单", icons: [{ glyph: "glyphicon-remove", handler: cancelOrder, data: "stock_code" }-->
                <!--] }-->

            ],
            pagesize: 100,
            maxPageButtons: 5,
            rowNumbers: true

        });



         dataTableCapital = jQuery("#capitalDataTable").raytable_tmp({
            datasource: { data: [], keyfield: 'stock_code' },
            columns: [

            { field: "account_id", title: "账户ID"},
                { field: "totalCapital", title: "总资产"},
                { field: "usableCapital", title: "可用资金"},
                { field: "freezeCapital", title: "冻结资金"}

            ],
            pagesize: 100,
            maxPageButtons: 5,
            rowNumbers: true

        });
    setTimeout(function () {
        refreshAllInfo();
    },400);

    setInterval(function () {
         refreshAllInfo();
    },30000);

    });


</script>
</body>
</html>

